package com.originskyseed.platform.user.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.mapper.PlayerRelateLabelFileMapper;
import com.originskyseed.platform.user.mapper.PlayerRelateLabelMapper;
import com.originskyseed.platform.user.model.PlayerRelateLabel;
import com.originskyseed.platform.user.model.PlayerRelateLabelFile;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.IPlayerRelateLabelService;

/**
 * 玩家标签表Service接口实现类（OSI_PLAYER_RELATE_LABEL）
 *
 * @author wlj
 */
@DubboService
@Component("playerRelateLabelService")
public class PlayerRelateLabelServiceImpl implements IPlayerRelateLabelService {

	@Resource
	private PlayerRelateLabelMapper playerRelateLabelMapper;

	@Resource
	private PlayerRelateLabelFileMapper playerRelateLabelFileMapper;

	/**
	 * 删除记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.playerRelateLabelMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.playerRelateLabelMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(PlayerRelateLabel record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.playerRelateLabelMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param playerRelateLabel
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(PlayerRelateLabel playerRelateLabel, List<PlayerRelateLabelFile> playerRelateLabelFileList, UserInfo userInfo) throws Exception {
		String key = PrimaryKeyGeneratorUtil.getInstance().getRandomKey();
		playerRelateLabel.setId(key);
		playerRelateLabel.setCreator(userInfo.getName());
		playerRelateLabel.setCreatorId(userInfo.getId());
		playerRelateLabel.setPath(null);
		playerRelateLabel.setCreateTime(new Date());
		if (playerRelateLabelFileList != null && playerRelateLabelFileList.size() > 0) {
			for (PlayerRelateLabelFile playerRelateLabelFile : playerRelateLabelFileList) {
				String key2 = PrimaryKeyGeneratorUtil.getInstance().getRandomKey();
				playerRelateLabelFile.setId(key2);
				playerRelateLabelFile.setCreateTime(new Date());
				playerRelateLabelFile.setPlayerId(playerRelateLabel.getPlayerId());
				if (playerRelateLabel.getAccount() != null) {
					playerRelateLabelFile.setAccount(playerRelateLabel.getAccount());
				}
				playerRelateLabelFile.setLabelId(key);
				this.playerRelateLabelFileMapper.insertSelective(playerRelateLabelFile);
			}
		}
		return this.playerRelateLabelMapper.insertSelective(playerRelateLabel);
	}

	/**
	 * 更新记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(PlayerRelateLabel record) throws Exception {
		record.setUpdateTime(new Date());
		return this.playerRelateLabelMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(PlayerRelateLabel record) throws Exception {
		record.setUpdateTime(new Date());
		return this.playerRelateLabelMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public PlayerRelateLabel selectByPrimaryKey(String id) throws Exception {
		return this.playerRelateLabelMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<PlayerRelateLabel> selectList(PlayerRelateLabel record) throws Exception {
		List<PlayerRelateLabel> playerRelateLabelList = this.playerRelateLabelMapper.selectList(record);
		if (playerRelateLabelList != null && playerRelateLabelList.size() > 0) {
			for (PlayerRelateLabel playerRelateLabel : playerRelateLabelList) {
				PlayerRelateLabelFile playerRelateLabelFile = new PlayerRelateLabelFile();
				playerRelateLabelFile.setLabelId(playerRelateLabel.getId());
				List<PlayerRelateLabelFile> playerRelateLabelFileList = this.playerRelateLabelFileMapper.selectList(playerRelateLabelFile);
				playerRelateLabel.setPlayerRelateLabelFileList(playerRelateLabelFileList);
			}
		}
		return playerRelateLabelList;
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<PlayerRelateLabel> selectListByPage(Map<String, Object> params, BasePage<PlayerRelateLabel> page) throws Exception {
		params.put("page", page);
		List<PlayerRelateLabel> list = this.playerRelateLabelMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

}
